package main

import (
	"flag"
	"fmt"
	"iron_man_comm/src/goroslib"
	"iron_man_comm/src/goroslib/dns"
	"iron_man_comm/src/goroslib/pkg/msgs/std_msgs"
	"iron_man_comm/src/msg/common_msg"
	"os"
	"time"
)

func main() {
	controlType := flag.Int("t", 0, "1:关机\n2:重启\n3:进入fct\n4:退出fct\n5:进入低功耗\n6:退出低功耗\n7:进入休眠\n8:退出休眠")
	flag.Parse()

	if 0 == *controlType {
		flag.PrintDefaults()
		return
	}
	fmt.Println("control type: ", *controlType)

	dns.GetDNSCacheInterface().UpdateDNSMapping("rk3562-buildroot", "127.0.0.1")
	node, err := goroslib.NewNode(goroslib.NodeConf{
		Name:          fmt.Sprintf("mcu_sleep_%d", time.Now().UnixMilli()),
		MasterAddress: "127.0.0.1:11311",
	})
	if err != nil {
		fmt.Println("create node fail, err: %s", err.Error())
		return
	}
	defer node.Close()

	publisher, err := goroslib.NewPublisher(goroslib.PublisherConf{
		Node:  node,
		Topic: "/serial_com_driver/control_power",
		Msg:   &common_msg.ControlPower{},
		Latch: true,
	})
	if err != nil {
		fmt.Println("create publish fail, err: %s", err.Error())
		return
	}
	defer publisher.Close()

	publisher.Write(&common_msg.ControlPower{
		Header: std_msgs.Header{
			Stamp:   time.Now(),
			FrameId: "",
		},
		Cmd: uint8(*controlType),
	})

	c := make(chan os.Signal, 1)
	_ = <-c
}
